
pub fn convert_base(num_str: &str, to_base: u32) -> String {
    let num_str = num_str.trim_end();
    let mut parts = num_str.splitn(2, '(');
    let nums = parts.next().unwrap();
    let from_base = parts.next().unwrap().trim_end_matches(')');
    let from_base = from_base.parse::<u32>().unwrap();
    if from_base < 2 || from_base > 16 || to_base < 2 || to_base > 16 {
        panic!("Base must be between 2 and 16.");
    }

    let mut decimal_num = 0;
    let mut power = 0;
    for digit in nums.chars().rev() {
        let value = if digit.is_digit(10) {
            digit.to_digit(from_base).unwrap()
        } else {
            10 + digit.to_ascii_uppercase() as u32 - 65
        };
        if value >= from_base {
            panic!("Invalid digit '{}' for base {}.", digit, from_base);
        }
        decimal_num += value * from_base.pow(power);
        power += 1;
    }

    if decimal_num == 0 {
        return "0".to_string();
    }

    let mut result = String::new();
    let mut remaining = decimal_num;
    while remaining > 0 {
        let remainder = remaining % to_base;
        let digit = if remainder < 10 {
            remainder.to_string()
        } else {
            (((remainder - 10) as u8 + b'a') as char)
           .to_string()
        };
        result.insert(0, digit.chars().next().unwrap());
        remaining /= to_base;
    }
    result
}
